var createError = require('http-errors');
var express = require('express');
var session = require('express-session');
var path = require('path');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var security = require('./utils/security');

var bodyParser = require('body-parser');
var router = require('./routes/router');
var mongoose = require('mongoose');

var systemSchedule = require('./service/schedule/SystemSchedule')

var app = express();

// 自定义token
logger.token('from', function(req, res){
  if(req.cookies && req.cookies['user']){
    return `${new Date()}-${req.cookies['user'].email}`;
  } else {
    return "-";
  }
  
});
// 自定义format，其中包含自定义的token
logger.format('joke', '[:from] :method :url :status');

// uncomment after placing your favicon in /public
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('joke'));
app.use(bodyParser.json({ "limit": "100000kb"}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(require('express-session')({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: false
}));

app.use(express.static(path.join(__dirname, 'public')));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').__express);
app.set('view engine', 'html');

// use session
app.use(session({
  secret:'secret',
  resave:true,
  saveUninitialized: false,
  cookie: {
    maxAge: 1000 * 60 * 30
  }
}));

// mongodb config
mongoose.connect('mongodb://127.0.0.1:27017/easywork', {
  useNewUrlParser: true,
  useUnifiedTopology: true
});
// mongoose.connect('mongodb://10.168.1.107:30017/easywork', {
//   useNewUrlParser: true,
//   useUnifiedTopology: true
// });
mongoose.connection.once('open', () => {
  console.log('connceted to database.')

  // schedule
  systemSchedule.excute()
});

// init router
app.use(function (req, res, next) {
  //安全检查
  if (security.process(req, res)) {
    if(router.routeRule(req, res)) {
      next();
    } else {
      console.log(401)
      res.sendStatus(401);
    }
  } else {
    res.sendStatus(417)
  }
});

router.initRoutes(app);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  console.log("error handler：" + err)
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.send({'code': 1, 'message': err.message})
});

module.exports = app;
